package com.kc.web.controller.business;

import com.kc.business.domain.BizGoodsCoupon;
import com.kc.business.service.IBizGoodsCouponService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 红包controller
 */
/**
 * 商品管理
 */
@RestController
@RequestMapping("/business/goodsCoupon")
public class BizGoodsCouponController extends BaseController {
    @Resource
    private IBizGoodsCouponService goodsCouponService;
    /**
     * 商品红包新增
     * @param bizGoodsCoupon
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCoupon:add')")
    @Log(title = "商品红包管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizGoodsCoupon bizGoodsCoupon) {
        bizGoodsCoupon.setStartDate(bizGoodsCoupon.getDateArr()[0]+" 00:00:00");
        bizGoodsCoupon.setEndDate(bizGoodsCoupon.getDateArr()[1]+" 23:59:59");
        return toAjax(goodsCouponService.add(bizGoodsCoupon));
    }

    /**
     * 商品红包修改
     * @param bizGoodsCoupon
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCoupon:edit')")
    @Log(title = "商品红包管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizGoodsCoupon bizGoodsCoupon) {
        bizGoodsCoupon.setStartDate(bizGoodsCoupon.getDateArr()[0]+" 00:00:00");
        bizGoodsCoupon.setEndDate(bizGoodsCoupon.getDateArr()[1]+" 23:59:59");
        return toAjax(goodsCouponService.edit(bizGoodsCoupon));
    }

    /**
     * 商品红包删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCoupon:remove')")
    @Log(title = "商品红包管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id) {
        BizGoodsCoupon bizGoodsCoupon = new BizGoodsCoupon();
        bizGoodsCoupon.setId(id);
        bizGoodsCoupon = goodsCouponService.getInfoById(bizGoodsCoupon);
        bizGoodsCoupon.setIsDeleted(1);
        return toAjax(goodsCouponService.edit(bizGoodsCoupon));
    }

    /**
     * 商品红包列表
     * @param bizGoodsCoupon
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCoupon:getList')")
    @GetMapping
    public TableDataInfo getList(BizGoodsCoupon bizGoodsCoupon) {
        startPage();
        return getDataTable(goodsCouponService.getList(bizGoodsCoupon));
    }

}
